/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Thread;

import Interface.IMain;
import StaticClass.ArrSocket;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.reflect.Array;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Oho
 */


public class Connection extends Thread {

    int port;
    JTable tb;
    ArrayList<StructClient> arrClient = new ArrayList<StructClient>();

    public Connection(int port, JTable tb,ArrayList<StructClient> arr) {
        this.port = port;
        this.tb = tb;
        this.arrClient=arr;
    }

    public void InitTable() {

        DefaultTableModel dtm = new javax.swing.table.DefaultTableModel();// moi control deu phai co 1 Model 
        //vd : jlist,jtable 
        //vi vay muon hien thi doi tuong gi len thi phai dung DefaultTableModel
        Vector colum_name = new Vector();//ds cot 
        Vector data_rows = new Vector();//gia tri tung dong 
        //for(int i=1;i<Colums;i++)
        //   colum_name.addElement(rsmeta.getColumnName(i));
        colum_name.addElement("Tên máy");//tao ten cót1 
        colum_name.addElement("IP");//cot 2
        colum_name.addElement("Trạng Thái");//..
        dtm.setColumnIdentifiers(colum_name);//them vao model de cho no hien cot len 
        //int i = 0;
        for(int i =0;i<ArrSocket.sockList.size();i++)
        {
            StructClient cl = arrClient.get(i);// duyet qua cau truc client
            data_rows.addElement(cl.getName());//lay ten ra 
            data_rows.addElement(ArrSocket.sockList.get(i).getLocalAddress());// sockList danh sach client
            //lay dia chi ...

            data_rows.addElement(cl.getState());
            dtm.addRow(data_rows);//themtat ca vao 1 dong 
        }
//        for (StructClient sk : arrClient) {//StructClient la 1 truct bao gom name,ip,...
//            //duyet qua danh sach cac cau truc cua client 
//            data_rows.addElement(sk.name);//lay ten ra 
//            data_rows.addElement(ArrSocket.sockList.get(i).getLocalAddress());// sockList danh sach client
//            //lay dia chi ...
//
//            data_rows.addElement(sk.state);
//            dtm.addRow(data_rows);//themtat ca vao 1 dong 
//            i++;//?? tai sao lai co i ???
//            // vi de xac dinh nhu sau :
//            //1. vd ban dau ta chi co 1 client 
//            //tuong ung sockList de duyet qua danh sach client thoi neu viet de hieu nhu z 
//        }
//        i = 0;
        tb.setModel(dtm);
        //this.repaint();

    }
    //ban dau ta can gan moi man hinh co 1 StructClient
//---------------vi vay de hien thi ten may len main tab2 thi ta chi can biet client vao may nao, 
    //tu do lay StructClient trong cua so do ra va hien thi len la xong 
    @Override
    public void run() {
        try {
            ServerSocket sv;
            sv = new ServerSocket(port);
            while (true) {
                Socket sk = sv.accept();
                //  sk.getl
                ObjectInputStream in = new ObjectInputStream(sk.getInputStream());
                ObjectOutputStream out = new ObjectOutputStream(sk.getOutputStream());// khi co 1 client ket noi den 
                ArrSocket.sockList.add(sk);
                ArrSocket.ArrConnect.add(this);//luu tien trinh nay lai 
                new ThreadChat(in, out).start();//tao tien trinh chat 

                // new ThreadMain(4567).start();//chat
                // new ThreadMain(7890).start();//send file
                // goi lai ham khoi tao jtable cua server
                //    ArrSocket.im = new IMain();
                InitTable();//khoi tao lai table
            }
        } catch (IOException ex) {
            Logger.getLogger(Connection.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
